using System;
using System.Collections.Generic;
using System.Text;

namespace ModWatch
{

	/// <summary>
	/// Manages a collection of <see cref="ModWatch.IModWatcher"/> implementations.
	/// </summary>
	public class ModManager
	{

		#region Properties

		private ModWatcherCollection _modWatchers;

		/// <summary>
		/// The collection of <see cref="ModWatch.IModWatcher"/>
		/// implementations being managed.
		/// </summary>
		public ModWatcherCollection ModWatchers
		{
			get { return _modWatchers; }
		}

		#endregion

		#region Constructors

		/// <summary>
		/// Initializes a new instance of the <see cref="ModWatch.ModManager"/> class.
		/// </summary>
		public ModManager()
		{
			_modWatchers = new ModWatcherCollection();
		}

		#endregion

		#region Save Methods

		/// <summary>
		/// Saves the current state of all <see cref="ModWatch.IModWatcher"/>
		/// objects being managed by this instance.
		/// </summary>
		public void SaveChanges()
		{

			foreach (IModWatcher mw in this.ModWatchers)
			{
				mw.SaveChanges();
			}
		}

		/// <summary>
		/// Saves the current state of all <see cref="ModWatch.IModWatcher"/>
		/// objects with the indicated ModGroup.
		/// </summary>
		/// <param name="ModGroup">
		///	The ModGroup to be saved.
		///	If null, all ModGroups will be saved.
		///	</param>
		public void SaveChanges(string ModGroup)
		{
			if (ModGroup == null)
				SaveChanges();

			foreach (IModWatcher mw in this.ModWatchers)
			{
				if (mw.ModGroup == ModGroup)
					mw.SaveChanges();
			}
		}

		#endregion

		#region IsDirty Methods

		/// <summary>
		/// Returns a value indicating whether any changes have been
		/// detected by any <see cref="ModWatch.IModWatcher"/> objects
		/// being managed by this instance.
		/// </summary>
		/// <returns>true if a change has been detected, false otherwise.</returns>
		public bool IsDirty()
		{

			foreach (IModWatcher mw in this.ModWatchers)
			{
				if (mw.IsDirty())
					return true;
			}
			return false;
		}

		/// <summary>
		/// Returns a value indicating whether any changes have been
		/// detected by any <see cref="ModWatch.IModWatcher"/> objects
		/// with the indicated ModGroup.
		/// </summary>
		/// <param name="ModGroup">
		///	The ModGroup to be checked.
		///	If null, all ModGroups will be checked.
		/// </param>
		/// <returns>true if a change has been detected, false otherwise.</returns>
		public bool IsDirty(string ModGroup)
		{
			if (ModGroup == null)
				return IsDirty();

			foreach (IModWatcher mw in this.ModWatchers)
			{
				if (mw.ModGroup == ModGroup)
				{
					if (mw.IsDirty())
						return true;
				}
			}
			return false;
		}

		#endregion

	}

}
